<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="应用ID" prop="appId">
        <el-input
          v-model="queryParams.appId"
          placeholder="请输入应用id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据类型" prop="pushDataType">
        <el-select v-model="queryParams.pushDataType" clearable>
          <el-option label="用户" value="user"/>
          <el-option label="部门" value="dept"/>
        </el-select>
      </el-form-item>
      <el-form-item label="推送事件" prop="pushEvent">
        <el-select v-model="queryParams.pushEvent" clearable>
          <el-option label="新增" value="add"/>
          <el-option label="修改" value="update"/>
          <el-option label="删除" value="delete"/>
        </el-select>
      </el-form-item>
      <el-form-item label="推送状态" prop="pushStatus">
        <el-select v-model="queryParams.pushStatus" clearable>
          <el-option label="待推送" :value="0"/>
          <el-option label="已推送" :value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item label="推送结果" prop="pushResult">
        <el-select v-model="queryParams.pushResult" clearable>
          <el-option label="成功" :value="1"/>
          <el-option label="失败" :value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataPushList">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="记录ID" align="center" prop="recordId" />
      <el-table-column label="应用ID" align="center" prop="appId" width="270" />
      <el-table-column label="数据类型" align="center" prop="pushDataType" >
        <template #default="scope">
          <span>{{ getPushDataTypeCN(scope.row.pushDataType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务ID" align="center" prop="bizId" />
      <el-table-column label="推送事件" align="center" prop="pushEvent" >
        <template #default="scope">
          <span>{{ getPushEventCN(scope.row.pushEvent) }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="推送的数据" align="center" prop="pushData" />-->
      <el-table-column label="推送状态" align="center" prop="pushStatus" >
        <template #default="scope">
          <span>{{ getPushStatusCN(scope.row.pushStatus) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="推送结果" align="center" prop="pushResult" >
        <template #default="scope">
          <span>{{ getPushResultCN(scope.row.pushResult) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="170">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Document" @click="handleDetail(scope.row)" v-hasPermi="['system:dataPush:query']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情对话框 -->
    <el-drawer title="详情" v-model="open" :size="'50%'" append-to-body>
      <div class="detail">
        <div class="detail-item">
          <div class="detail-item-title">记录ID：</div>
          <div class="detail-item-value">{{ form.recordId }}</div>
        </div>
        <div class="detail-item">
          <div class="detail-item-title">应用ID：</div>
          <div class="detail-item-value">{{ form.appId }}</div>
        </div>
        <div class="detail-item">
          <div class="detail-item-title">创建时间：</div>
          <div class="detail-item-value">{{ parseTime(form.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</div>
        </div>
        <div class="detail-item">
          <div class="detail-item-title">数据类型：</div>
          <div class="detail-item-value">{{ getPushDataTypeCN(form.pushDataType) }}</div>
        </div>
        <div class="detail-item">
          <div class="detail-item-title">业务ID：</div>
          <div class="detail-item-value">{{ form.bizId }}</div>
        </div>
        <div class="detail-item">
          <div class="detail-item-title">推送事件：</div>
          <div class="detail-item-value">{{ getPushEventCN(form.pushEvent) }}</div>
        </div>
        <div class="detail-item">
          <div class="detail-item-title">推送状态：</div>
          <div class="detail-item-value">{{ getPushStatusCN(form.pushStatus) }}</div>
        </div>
        <div class="detail-item">
          <div class="detail-item-title">推送结果：</div>
          <div class="detail-item-value">{{ getPushResultCN(form.pushResult) }}</div>
        </div>
        <div class="detail-item">
          <div class="detail-item-title">推送数据：</div>
          <div class="detail-item-value">{{ form.pushData }}</div>
        </div>
        <div class="detail-item">
          <div class="detail-item-title">推送日志：</div>
          <div class="detail-item-value">{{ form.pushLog }}</div>
        </div>

      </div>
    </el-drawer>
  </div>
</template>

<script setup name="DataPush">
import { listDataPush, getDataPush } from "@/api/system/dataPush";

const { proxy } = getCurrentInstance();

const dataPushList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    appId: null,
    pushDataType: null,
    bizId: null,
    pushEvent: null,
    pushData: null,
    pushStatus: null,
    pushResult: null,
    pushLog: null,
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询数据推送记录列表 */
function getList() {
  loading.value = true;
  listDataPush(queryParams.value).then(response => {
    dataPushList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.recordId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/**
 * 获取推送的数据类型中文
 * @param type
 * @returns {string}
 */
function getPushDataTypeCN(type) {
  switch (type) {
    case 'user':
      return '用户'
    case 'dept':
      return '部门'
    default:
      return '未知'
  }
}

/**
 * 获取推送事件中文
 * @param type
 * @returns {string}
 */
function getPushEventCN(event) {
  switch (event) {
    case 'add':
      return '新增'
    case 'update':
      return '修改'
    case 'delete':
      return '删除'
    default:
      return '-'
  }
}

/**
 * 获取推送状态中文
 * @param type
 * @returns {string}
 */
function getPushStatusCN(event) {
  switch (event) {
    case 0:
      return '待推送'
    case 1:
      return '已推送'
    default:
      return '-'
  }
}

/**
 * 获取推送结果中文
 * @param type
 * @returns {string}
 */
function getPushResultCN(event) {
  switch (event) {
    case 1:
      return '成功'
    case 2:
      return '失败'
    default:
      return '-'
  }
}

/** 详情按钮操作 */
function handleDetail(row) {
  getDataPush(row.recordId).then(response => {
    form.value = response.data;
    open.value = true;
  });
}
getList();
</script>
<style lang='less' scoped>
.detail {

}
.detail-item {
  display: flex;
  flex-direction: row;
  margin-bottom: 16px;
  font-size: 14px;
}
.detail-item-title {
  text-align: right;
  width: 80px;
  font-weight: bolder;
  color: #606266
}
.detail-item-value {
  flex: 1;
  padding-left: 10px;
  word-break: break-all;
}
</style>